﻿using System.Web;
using NHibernate;

namespace _5E.NHibernateRepository.SessionStorage
{
    public class HttpSessionContainer : ISessionStorageContainer
    {
        private const string SESSION_KEY = "NHibernateSession";

        public ISession CurrentSession
        {
            get
            {
                ISession session = null;

                if (HttpContext.Current.Items.Contains(SESSION_KEY))
                    session = (ISession)HttpContext.Current.Items[SESSION_KEY];

                return session;
            }
        }

        public void Store(ISession session)
        {
            if (HttpContext.Current.Items.Contains(SESSION_KEY))
                HttpContext.Current.Items[SESSION_KEY] = session;
            else
                HttpContext.Current.Items.Add(SESSION_KEY, session);
        }
    }
}
